package Person_JavaBean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.SQLException;

public class UserDao {
    public User findInfoByname(String name) throws Exception{
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        try {
//            Class.forName("com.mysql.jdbc.Driver");
//            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bloody_characters_db?characterEncoding=GBK","root","root");
//            Statement state = conn.createStatement();
//            String sql = "select * from db where name='郑和';";
//            rs = state.executeQuery(sql);
            conn = JdbcUtil.getConnection();
            String sql = "select * from db where name =?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,name);
            rs = ps.executeQuery();
            if (rs.next()){
                user = new User(rs.getLong(1),rs.getString(2),rs.getInt(3),rs.getInt(4),rs.getInt(5),rs.getInt(6),rs.getInt(7),rs.getString(8),rs.getBinaryStream(9));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            JdbcUtil.free(rs,ps,conn);
        }
        return user;
    }
}
